package com.github.bluesbruce.websocket;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;

/**
 * 消息实体
 *
 * @author BBF
 */
public class WebSocketMessage implements Serializable {

  private static final long serialVersionUID = -3716732853962569920L;

  private String msg;

  public WebSocketMessage() {
  }

  public WebSocketMessage(String msg) {
    this.msg = msg;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  /**
   * 序列化
   *
   * @return JSON字符串
   */
  public String toJson() {
    try {
      ObjectMapper mapper = new ObjectMapper();
      return mapper.writeValueAsString(this);
    } catch (JsonProcessingException ex) {
      return "{\"msg\":\"\"}";
    }
  }
}
